package work.huangxin.share.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import work.huangxin.share.constant.RedisConstant;
import work.huangxin.share.domain.other.UserMessage;
import work.huangxin.share.domain.other.WXSessionModel;
import work.huangxin.share.service.impl.UserMessageOperationService;

import javax.servlet.http.HttpServletRequest;

/**
 * 检查是否拉黑
 */

@Component
public class CheckAllow {

    @Autowired
    private RedisUtil redisUtil;

    public  Integer checkAllow(UserMessageOperationService userMessageOperationService,
                               HttpServletRequest request) {
        WXSessionModel user = (WXSessionModel) request.getSession().getAttribute("user");
        Long userId = ThreadLocalUtil.get();
        WXSessionModel user1 = (WXSessionModel) redisUtil.get("user:" + userId);

        //在redis上面找，用户是否被拉黑。若在redis上找得到，则表明被拉黑了，若找不到，则标识用户没被拉黑
        if (redisUtil.sHasKey(RedisConstant.USER_BLOCK_LIST + user.getUserId(), "")) {
            return -1;
        }
//        if (userMessageOperationService.getById(user.getUserId()).getUserAllow().equals("2")) {
//            return -1;
//        }
        return user.getUserId();
    }

    public UserMessage getUserMessage(UserMessageOperationService userMessageOperationService, HttpServletRequest request) {
        WXSessionModel user = (WXSessionModel) request.getSession().getAttribute("user");

        Long userId = ThreadLocalUtil.get();
        WXSessionModel user1 = (WXSessionModel) redisUtil.get("user:" + userId);

        UserMessage userMessage = userMessageOperationService.getById(user.getUserId());
        if (userMessage.getUserAllow().equals("2")) {
            return null;
        }
        return userMessage;
    }
}
